<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 
        判断obj1中是否存在eat这个属性
     -->
</head>

<body>
    <input type="text" name="" id="test">
</body>
<script>
    let obj1 = document.getElementById('test');
    let attributeName = 'eat';
    obj1.setAttribute('eat', 'apple'); //添加eat属性
    if (existAttribute(obj1, 'eat')) {
        alert('存在' + attributeName + '属性');
    } else {
        alert('不存在' + attributeName + '属性 ！');

    }


    //存在某属性
    function existAttribute(obj, attributeName) {
        let flag = false;
        let attrArr = obj.getAttributeNames();
        for (let i = 0; i < attrArr.length; i++) {
            // console.log(attrArr[i],attributeName);//测试
            if (attrArr[i] == attributeName) {
                console.log(1111);
                flag = true; //存在属性
            }
        }
        return flag;
    }
</script>

</html>